package main

/**
** @ Author       : SmallSuperMan
** @ Date         : 2021-02-17 22:14:31
** @ LastEditTime : 2022-05-10 23:13:13
** @ LastEditors  : SmallSuperMan
** @ FilePath     : \CheckFileMD5\main.go
** @ Description  :
**/

import (
	"fmt"
	"log"
	"path/filepath"

	// "path/filepath"
	"strings"
)

func failedNoExits(msg string) {
	// 防止立即退出
	var str1 string
	log.Printf("%s \n 输入回车退出！！！", msg)
	fmt.Scanln(&str1)
}

func main() {

	fileMD5Map01 := map[string]string{}
	fileMD5Map02 := map[string]string{}
	// filePathMD5Map :=make(map[string]map[string]string,2)
	folderName01, folderName02, compareFlag, relativeFlag, detDiffFileSuff := getParams()
	log.Printf("folderName01=%s, folderName02=%s, compareFlag=%s relativeFlag=%s detDiffFileSuff=%s", folderName01, folderName02, compareFlag, relativeFlag, detDiffFileSuff)

	fileList01 := []string{}

	getFiles(folderName01, &fileList01)

	for _, filePathName := range fileList01 {
		fileMD5, _ := getFileMD5(filePathName)
		key := strings.Replace(filePathName, folderName01, "", 1)
		if relativeFlag == "0" {
			key = filepath.Base(filePathName)
		}
		fileMD5Map01[key] = fileMD5
	}

	fileList02 := []string{}
	getFiles(folderName02, &fileList02)
	for _, filePathName := range fileList02 {
		fileMD5, _ := getFileMD5(filePathName)
		key := strings.Replace(filePathName, folderName02, "", 1)
		if relativeFlag == "0" {
			key = filepath.Base(filePathName)
		}
		fileMD5Map02[key] = fileMD5
	}
	// 源头存在，目标不存在
	source1Target0 := ""
	// 两个目录均存在，内容不一致
	source1Target1Inf0 := ""
	// 记录差异文件
	difFileList := []string{}

	for key, value := range fileMD5Map01 {

		v, ok := fileMD5Map02[key]
		if ok {
			if v == value {
				log.Printf("[INFO]文件名:%s两个目录均存在且内容一致！", key)
			} else {
				source1Target1Inf0 += fmt.Sprintf("[ERROR:]文件名: %s 两个目录均存在,但文件内容不一致！\n", key)
				difFileList = append(difFileList, key)
			}
		} else {
			source1Target0 += fmt.Sprintf("[ERROR:]文件名: %s 目录 %s 存在，但是目录 %s 不存在\n", key, folderName01, folderName02)
		}
	}
	//源头不存在，目标存在
	source0Target1 := ""
	for key := range fileMD5Map02 {
		_, ok := fileMD5Map01[key]
		if !ok {
			source0Target1 += fmt.Sprintf("[ERROR:]文件名: %s 目录 %s 存在，但是目录 %s 不存在\n", key, folderName02, folderName01)
		}
	}

	switch compareFlag {
	case "1":
		tmpLog := source1Target0 + source0Target1 + source1Target1Inf0
		if tmpLog == "" {
			tmpLog = "[INFO]:两目录文件数量以及内容一致!"
		}
		log.Printf("\n" + tmpLog)
	case "2":
		tmpLog := source1Target0 + source1Target1Inf0
		if tmpLog == "" {
			tmpLog = "[INFO]:源目录文件在目标目录均存在且内容一致!"
		}
		log.Printf("\n" + tmpLog)
	case "3":
		tmpLog := source0Target1 + source1Target1Inf0
		if tmpLog == "" {
			tmpLog = "[INFO]:目标目录文件在源目录均存在且内容一致!"
		}
		log.Printf("\n" + tmpLog)
	}
	// 获取了所有差异文件,并根据后缀判断是否比较内容
	log.Printf("****************************************差异文件内容输出-【begin】****************************************")

	for index, fileName := range difFileList {
		// 如果传入的不考虑相对路径的就比较明细
		if relativeFlag == "0" {
			log.Printf("[INFO]:当前配置参数不考虑相对路径，所以不比较明细内容")
			break
		}

		if !IsExistsStringInSlice(GetFileFullNameSuffixNamePrefixName(fileName).SuffixName, strings.Split(detDiffFileSuff, "|")) {
			log.Printf("第【%d】个差异文件是【%v】,不检查明细内容！", index+1, fileName)
			continue
		}
		log.Printf("第【%d】个差异文件是【%v】", index+1, fileName)
		// 文件全路径
		currDelimiter := getPathDelimiter()
		if relativeFlag == "1" {
			currDelimiter = ""
		}

		filePath01 := folderName01 + currDelimiter + fileName
		filePath02 := folderName02 + currDelimiter + fileName

		// ReadFileFromLine(filePath01, func(s string) {
		// 	log.Printf("%v测试按行读取--->[%v]<---", filePath01, GetStringMD5(strings.ReplaceAll(s, "\n", "")))
		// })
		// ReadFileFromLine(filePath02, func(s string) {
		// 	log.Printf("%v测试按行读取--->[%v]<---", filePath02, GetStringMD5(strings.ReplaceAll(s, "\n", "")))
		// })

		// 两个文件差异 -1 文件1独有  0两个文件都有 1 文件2独有
		// log.Printf("\n----------------\n%v\n----------------", GetFileFullNameSuffixNamePrefixName(filePath01))
		diffs := GetTowStringDiff(ReadFileToString(filePath01), ReadFileToString(filePath02))
		// log.Printf("【%v】--->类型为[%T],[%v]<---", fileName, diffs, diffs)
		tmpCount01 := 1
		tmpText01 := ""
		tmpCount02 := 1
		tmpText02 := ""
		for _, tmpDif := range diffs {
			switch tmpDif.Type {
			case -1:
				// log.Printf("【%v】第 %d 处特有:【%v】", filePath01, tmpCount01, tmpDif.Text)
				tmpCount01++
				tmpText01 += tmpDif.Text
			case 1:
				// log.Printf("【%v】第 %d 处特有:【%v】", filePath02, tmpCount02, tmpDif.Text)
				tmpCount02++
				tmpText02 += tmpDif.Text
			default:

			}
		}

		log.Printf("【%v】特有:【%v】", filePath01, tmpText01)
		log.Printf("【%v】特有:【%v】", filePath02, tmpText02)

		// // v, _ := ArrSliceToMapSet([]string{"1", "2", "4", "1"})
		// log.Printf("值【%#v】类型【%T】", IsExistsStringInSlice("0", []string{"1", "2", "4", "1"}), IsExistsStringInSlice("5", []string{"1", "2", "4", "1"}))

	}
	log.Printf("****************************************差异文件内容输出-【end】****************************************")

	if getPathDelimiter() == "\\" {
		failedNoExits("程序结束！")
	}

}
